#if UNITY_EDITOR
using System.IO;
using UnityEditor;
using UnityEngine;

public class EditorScreenshot : EditorWindow
{
    private int width = 1920;
    private int height = 1080;
    private string fileName = "EditorScreenshot";
    
    [MenuItem("Tools/场景截图")]
    public static void ShowWindow()
    {
        GetWindow<EditorScreenshot>("场景截图工具");
    }
    
    void OnGUI()
    {
        GUILayout.Label("截图设置", EditorStyles.boldLabel);
        width = EditorGUILayout.IntField("宽度", width);
        height = EditorGUILayout.IntField("高度", height);
        fileName = EditorGUILayout.TextField("文件名", fileName);
        
        if (GUILayout.Button("捕获当前场景视图"))
        {
            CaptureSceneView();
        }
    }
    
    void CaptureSceneView()
    {
        // 获取当前场景视图
        SceneView sceneView = SceneView.lastActiveSceneView;
        Camera sceneCam = sceneView.camera;
        
        // 设置渲染纹理
        RenderTexture rt = new RenderTexture(width, height, 24);
        sceneCam.targetTexture = rt;
        
        // 渲染
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
        sceneCam.Render();
        RenderTexture.active = rt;
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        tex.Apply();
        
        // 保存文件
        byte[] bytes = tex.EncodeToPNG();
        string path = EditorUtility.SaveFilePanel("保存截图", "", fileName, "png");
        if (!string.IsNullOrEmpty(path))
        {
            File.WriteAllBytes(path, bytes);
            Debug.Log($"场景截图已保存: {path}");
        }
        
        // 清理
        sceneCam.targetTexture = null;
        RenderTexture.active = null;
        DestroyImmediate(rt);
        DestroyImmediate(tex);
    }
}
#endif